package com.ruowei.modules.sys.repository;

import com.ruowei.modules.sys.domain.Todo;
import com.ruowei.modules.sys.web.dto.TodoDTO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Spring Data  repository for the Todo entity.
 */
@SuppressWarnings("unused")
@Repository
public interface TodoRepository extends JpaRepository<Todo, Long> {

    @Query("select t.type as type, count(t.id) as number from Todo t where t.userCode = ?1 group by t.type")
    List<TodoDTO> getAllMineTodos(String userCode);

    @Modifying
    @Query(nativeQuery = true,
        value = "delete from TODO t" +
            " where t.id = (select d.id from TODO d where d.USER_CODE = ?1 and d.TYPE = ?2 and ROWNUM <= 1)")
    void deleteFirstByUserCodeAndType(String userCode, String type);

    void deleteAllByUserCodeIn(List<String> userCodes);
}
